package com.gzu.shop.recommend;

import com.gzu.shop.entity.Thing;
import com.gzu.shop.mapper.ThingMapper;
import com.gzu.shop.util.MyTextComparator;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;


@Component
public class ScheduledTasks {
    @Autowired
    private ThingMapper thingMapper;
    @Scheduled(cron = "0 0 3 ? * MON") // 每周一早上3点执行一次,计算物品相私度
    public  void calculateSimilarity() {
        List<Thing> thing = thingMapper.selectAll();
        // 如果结果为空
        if (thing.isEmpty()) {
            return;
        }
        for (int i = 0; i < thing.size(); i++) {
            for (int j = i + 1; j < thing.size(); j++) {
                  // 计算相似度
                 // 根据商品的形容description, 商品名title来计算相似度
                double desSimi = MyTextComparator.getCosineSimilarity(thing.get(i).getDescription(), thing.get(j).getDescription());
                double titSimi = MyTextComparator.getCosineSimilarity(thing.get(i).getTitle(), thing.get(j).getTitle());
                double thingSimilarity = desSimi + titSimi;
                Long t1Id = thing.get(i).getId();
                Long t2Id = thing.get(j).getId();
                thingMapper.insertSimilarity(t1Id, t2Id,thingSimilarity);
            }
        }
    }
}
